package com.shop.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shop.Utils.PageBean;
import com.shop.Utils.ResponseMessage;
import com.shop.po.Orderitem;
import com.shop.po.Orders;
import com.shop.po.Product;
import com.shop.po.Shopcart;
import com.shop.po.User;
import com.shop.service.OrderService;
import com.shop.service.ShopcartService;


@Controller
public class orderController {
	@Autowired
	private OrderService orderService;

	@Autowired
	private ShopcartService shopcartService;
	
	//跳转到订单界面
	@RequestMapping("/toOrder")
	public String toOrder(HttpServletRequest request,Model model) throws Exception {
		Orders orders = new Orders();
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		if(loginUser==null){
			model.addAttribute("message", "对不起您还没有登录");
			return "msg";
		}
//		0表示没有付款、1表示已付款即将发货 2表示确认收货 3表示交易成功
		orders.setState(0);
		orders.setOrderTime(new Date());
		orders.setUid(loginUser.getUid());

		List<Shopcart> cartItems = shopcartService.selCartByUid(loginUser.getUid());
		Double totale=0.0;
		for (Shopcart sc : cartItems) {
			totale+=sc.getSubtotle();
		}
		orders.setMoney(totale);
		
		orderService.toOrder(orders);
		for (Shopcart entry : cartItems) {
			Orderitem orderitem = new Orderitem();
			Product product=new Product();
			product.setImage(entry.getPimage());
			product.setPname(entry.getPname());
			product.setShopPrice(entry.getPprice());
			orderitem.setProduct(product);
			orderitem.setCount(entry.getCount());
			orderitem.setPid(entry.getPid());
			orderitem.setSubtotal(entry.getSubtotle());
			orderitem.setOid(orders.getOid());
			orders.getOiList().add(orderitem);
			orderService.toOrderItem(orderitem);
		}
		shopcartService.deleteByUid(loginUser.getUid());
		request.getSession().setAttribute("orders", orders);
		return "order";
	}

	// 为定单付款
	@RequestMapping("/payOrder")
	public String payOrder(Orders orders,@RequestParam String receiveInfo,@RequestParam String phoNum,@RequestParam String accepter) throws Exception {
		orders.setReceiveinfo(receiveInfo);
		orders.setPhonum(phoNum);
		orders.setAccepter(accepter);
		orderService.payOrder(orders);
		return "redirect:myOrder.action?page=1";
	}
	
   // 再次进入付款界面
	@RequestMapping("/payOrderAganin")
	public String payOrderAganin(@RequestParam int oid,HttpServletRequest request){
		Orders noPayOrder = orderService.findOrderByOid(oid);
		request.getSession().setAttribute("orders", noPayOrder);
		return "order";
	}
	// 查询我的订单
	@RequestMapping("/myOrder")
	public String myOrder(@RequestParam int page, Model model,
			HttpServletRequest request) throws Exception {
		User loginUser = (User) request.getSession().getAttribute("loginUser");
		PageBean<Orders> pageBean = orderService.findOrderByUidAndPage(page,loginUser.getUid());
		System.out.println(pageBean.toString());
		model.addAttribute("pageBean", pageBean);
		return "orderList";
	}
	
	// 确认收货
	@RequestMapping("/updateState")
	public String updateState(@RequestParam int oid ) throws Exception {
		orderService.updateOrderStatus(oid, 3);
		return "redirect:myOrder.action?page=1";
	}
	
	// 删除订单
	@RequestMapping("/delOrder")
	public String delOrder(@RequestParam int oid, HttpServletRequest request, Model model) throws Exception {
		orderService.delOrders(oid);
		return "redirect:myOrder.action?page=1";
	}
	
	//批量删除
	@RequestMapping("/deleteOrders.action")
	@ResponseBody
	public String deleteMessage(String deleteList)throws Exception{
		//因为传递的deleteList为带","的字符串,所以需要消掉分隔符转为数组
		String[] ids = deleteList.split(",");
		for(String id:ids){
			orderService.delOrders(Integer.parseInt(id));
		}
		return "1";
	}	

	@RequestMapping(value = "/checkEntry", produces = { "application/json;charset=UTF-8" }, method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage checkEntry(HttpServletRequest request,@RequestParam String receiveInfo,@RequestParam String phoNum,@RequestParam String accepter) throws Exception {
		if(receiveInfo==null||receiveInfo==""||accepter==null||accepter==""||phoNum==null||phoNum=="") {
			return ResponseMessage.error().addObject("yanzheng", "请将收货信息填写完整!");
			
		}
		return ResponseMessage.success().addObject("yanzheng", "收获信息填写完成!");
	}
	
	//查看商品订单详情
		@RequestMapping("/orderDetails")
		public String orderDetails(Model model, HttpServletRequest request,@RequestParam int oid) throws Exception {
			List<Orders> orders = orderService.selectorderByPrimaryKey(oid);
			Orders order=orders.get(0);
			
			Date currentTime = order.getOrderTime();
			SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String strTime=formatter.format(currentTime);
			
			request.getSession().setAttribute("strTime", strTime);
			System.out.println(orders.toString());
			request.getSession().setAttribute("orders", order);
			return "orderDetails";
		}
}

